<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.note.pojo.Notetype" %>
<%@include file="/jsp/common/head.jsp"%>
<div class="right">
    <div class="location">
        <strong>你现在所在的位置是:</strong>
        <span>分类管理页面</span>
    </div>
    <table class="providerTable" cellpadding="0" cellspacing="0">
        <tr class="firstTr">
            <td>编号</td>
            <td>类型</td>
            <td>操作</td>
        </tr>
        <%
            List<Notetype> typelist = (List<Notetype>) request.getAttribute("typelist");
            for (Notetype notetype : typelist) {
        %>
        <tr>
            <td><%=notetype.getTypeid()%></td>
            <td><%=notetype.getTypename()%></td>
            <td><a href="${pageContext.request.contextPath}/deleteType?typeId=<%=notetype.getTypeid()%>" onclick="return deleteOne()">删除</a></td>
        </tr>
        <%
            }
        %>
    </table>
    <div style="position: absolute; right: 25px;margin-top: 10px">
        <input type="text" name="type" style="height: 30px" id="typeName" onkeyup="this.value=this.value.replace(/[, ]/g,'')"
        >
        <button style="height: 30px;width: 50px" id="btn1">添加</button>
    </div>
</div>
</section>
<%@include file="/jsp/common/foot.jsp" %>
<script type="text/javascript">
    function deleteOne() {
        var msg = "删除可能会连带删除文章?\n请慎重考虑！";
        if (confirm(msg)==true){
            return true;
        }else{
            return false;
        }
    }
    $("#btn1").click(function(){
        var $typename = $("#typeName").val();
        var $index = $(".providerTable").find('tr:last-child').find('td').eq(0)
        if($typename!=""){
            $.ajax({
                url:"${pageContext.request.contextPath}/addType",
                data:{"typeName":$typename},
                type:"post",
                success:function (data){
                    var obj = JSON.parse(data)
                    if(obj.flag="true"){
                        $(".providerTable").append("<tr><td>"+obj.typeid+"</td><td>"+$typename+"</td><td><a href='${pageContext.request.contextPath}/deleteType?typeId="+obj.typeid+"' onclick='return deleteOne()'>删除</a></td></tr>");
                    }
                }
            })
        }else{
            $("#typeName").attr("placeholder","请输入内容");
        }
    })
</script>
